package com.SouthernWall_404.age_tool.common.Network.Packets.S2C;

import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.ClientHandler.MusicBlockStartPlayClientHandler;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class MusicBlockStartPlayS2CPacket {

    public boolean trigger;
    public BlockPos blockPos;

    public MusicBlockStartPlayS2CPacket(boolean trigger, BlockPos blockPos)
    {
        this.blockPos=blockPos;
        this.trigger = trigger;
    }

    public MusicBlockStartPlayS2CPacket(FriendlyByteBuf buf) {
        this.blockPos=buf.readBlockPos();
        this.trigger =buf.readBoolean();
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeBlockPos(this.blockPos);
        buf.writeBoolean(this.trigger);
    }
    public void handle(Supplier<NetworkEvent.Context> ctx)
    {
        ctx.get().enqueueWork(() ->
                // 确保其仅在物理客户端上执行
                DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> MusicBlockStartPlayClientHandler.play(this))
        );
        ctx.get().setPacketHandled(true);
    }
}
